package cn.com.dhcc.app.pub.module.codematic.service;


import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

import org.springframework.util.ResourceUtils;

import cn.com.dhcc.app.core.util.FreemarkerUtil;
import cn.com.dhcc.app.pub.module.codematic.vo.Bean;

public class ViewGenerator{
	public static void generate(String appRoot,Class<?> clazz, Bean entity) throws Exception {
		File template = ResourceUtils.getFile("classpath:template/codematic/view_list_template.ftl");
		File pkg =  new File(appRoot + "/codematic/main/webapp/WEB-INF/view/jsp/"+entity.getViewPkg());
		if(!pkg.exists()){
			pkg.mkdirs();
		}
		Writer fileWriter = new FileWriter(new File(pkg.getAbsoluteFile() + "/" + entity.getBeanUri()+ "_list.jsp"));
		FreemarkerUtil.flushData(template.getAbsolutePath(), fileWriter, entity);
		
		File formTpl = ResourceUtils.getFile("classpath:template/codematic/view_form_template.ftl");
		Writer formViewWriter = new FileWriter(new File(pkg.getAbsoluteFile() + "/" + entity.getBeanUri()+ "_form.jsp"));
		FreemarkerUtil.flushData(formTpl.getAbsolutePath(), formViewWriter, entity);
		
		
		File detailTpl = ResourceUtils.getFile("classpath:template/codematic/view_detail_template.ftl");
		Writer detailViewWriter = new FileWriter(new File(pkg.getAbsoluteFile() + "/" + entity.getBeanUri()+ "_detail.jsp"));
		FreemarkerUtil.flushData(detailTpl.getAbsolutePath(), detailViewWriter, entity);
	}
}
